/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import type { StringObjType } from '@/components/_pages/AmountApply/index.d';
import { checkAndTriggerRefresh } from '@/components/_pages/red-point/_utils';
import request from '@/utils/request';
import { message } from 'antd';
import { isArray } from 'lodash';

export async function getCloudCallTemplateList(
  params: StringObjType,
  fromRedDot?: boolean,
): Promise<any> {
  return request('/CustomerServiceV2/ivrTemplate', {
    method: 'POST',
    data: params,
  })
    .then(({ code, data, msg }) => {
      if (code != 0) {
        message.error(msg);
      }
      const list = (isArray(data.list) ? data.list : []).map(
        (item: StringObjType, index: number) => ({
          id: index + 1,
          ...item,
        }),
      );
      return {
        data: list,
        total: data.recordTotal,
      };
    })
    .then(checkAndTriggerRefresh(getCloudCallTemplateList, fromRedDot));
}
export async function getCloudCallBlackList(params: StringObjType): Promise<any> {
  return request('/cloudCall/blackList', {
    method: 'POST',
    data: params,
  }).then(({ code, data, msg }) => {
    if (code != 0) {
      message.error(msg);
    }
    const list = (isArray(data.list) ? data.list : []).map(
      (item: StringObjType, index: number) => ({
        id: index + 1,
        ...item,
      }),
    );
    return {
      data: list,
      total: data.pageTotal,
    };
  });
}

export async function templateCheck(params: StringObjType): Promise<any> {
  return request('/cloudCall/templateCheck', {
    method: 'POST',
    data: params,
  }).then(({ code, msg }) => {
    if (code != 0) {
      message.error(msg);
    }
    return {
      code,
    };
  });
}
export async function templateReject(params: StringObjType): Promise<any> {
  return request('/cloudCall/templateReject', {
    method: 'POST',
    data: params,
  }).then(({ code, msg }) => {
    if (code != 0) {
      message.error(msg);
    }
    return {
      code,
    };
  });
}
export async function templateBlack(params: StringObjType): Promise<any> {
  return request('/cloudCall/templateBlack', {
    method: 'POST',
    data: params,
  }).then(({ code, msg }) => {
    if (code != 0) {
      message.error(msg);
    }
    return {
      code,
    };
  });
}
export async function moveoutBlackList(params: StringObjType): Promise<any> {
  return request('/cloudCall/moveoutBlackList', {
    method: 'POST',
    data: params,
  }).then(({ code, msg }) => {
    if (code != 0) {
      message.error(msg);
    }
    return {
      code,
    };
  });
}
